//
//  LMTabBarViewController.m
//  Conva
//
//  Created by cruise on 2018/1/6.
//  Copyright © 2018年 com.conva. All rights reserved.
//

#import "LMTabBarViewController.h"

#import "LMNavigationController.h"

#import "CAOnlineViewController.h"
#import "CAOffineViewController.h"
#import "CAInterflowViewController.h"
#import "CAHomeViewController.h"
#import "CAProfileViewController.h"

#import <objc/runtime.h>

@interface LMTabBarViewController ()

@end

@implementation LMTabBarViewController

#pragma mark - 第一次使用当前类的时候对设置UITabBarItem的主题
+ (void)initialize {
    
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 90000
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedIn:self, nil];
#else
    // For iOS 9+
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
#endif
    
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = RGBCOLOR(147, 147, 147);
    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = RGBCOLOR(194, 101, 183);
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initControllers];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化子控制器
-(void) initControllers {
    
    [self setupController:[[CAOnlineViewController alloc] init]
                    image:@"tabbar_online_nomal"
            selectedImage:@"tabbar_online_selected"
                    title:@"ONLINE"];
    
    [self setupController:[[CAOffineViewController alloc] init]
                    image:@"tabbar_offline_nomal"
            selectedImage:@"tabbar_offline_selected"
                    title:@"OFFLINE"];
    
    [self setupController:[[CAHomeViewController alloc] init]
                    image:@"tabbar_explore_nomal"
            selectedImage:@"tabbar_explore_selected"
                    title:@"EXPLORE"];
    
    [self setupController:[[CAInterflowViewController alloc] init]
                    image:@"tabbar_interflow_nomal"
            selectedImage:@"tabbar_interflow_selected"
                    title:@"INTERFLOW"];
    
    [self setupController:[[CAProfileViewController alloc] init]
                    image:@"tabbar_profile_nomal"
            selectedImage:@"tabbar_profile_selected"
                    title:@"PROFILE"];
}

/**
 *
 *  设置单个tabBarButton
 *
 *  @param vc               每一个按钮对应的控制器
 *  @param image      每一个按钮对应的普通状态下图片
 *  @param selectedImage    每一个按钮对应的选中状态下的图片
 *  @param title            每一个按钮对应的标题
 */
- (void) setupController:(UIViewController *)vc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title {
    
    vc.tabBarItem.image = [UIImage imageNamed:image];
    
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    vc.tabBarItem.selectedImage = mySelectedImage;
    vc.tabBarItem.title = title;
    
    LMNavigationController *nav = [[LMNavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
    
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    [super setSelectedIndex:selectedIndex];
//    [self beginAnimation];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//    [self beginAnimation];
}

#pragma mark - 动画
- (void)beginAnimation {
    CATransition *animation         = [[CATransition alloc]init];
    animation.duration              = 0.5;
    animation.type                  = kCATransitionFade;
    animation.subtype               = kCATransitionFromRight;
    animation.timingFunction        = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    animation.accessibilityFrame    = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [self.view.layer addAnimation:animation forKey:@"switchView"];
}

@end

@implementation NSObject (LMTabBarViewController)

- (LMTabBarViewController *)lm_tabBarController {
    
    LMTabBarViewController *tabBarController = objc_getAssociatedObject(self, @selector(lm_tabBarController));
    if (tabBarController) {
        return tabBarController;
    }
    if ([self isKindOfClass:[UIViewController class]] && [(UIViewController *)self parentViewController]) {
        tabBarController = [[(UIViewController *)self parentViewController] lm_tabBarController];
        return tabBarController;
    }
    id<UIApplicationDelegate> delegate = ((id<UIApplicationDelegate>)[[UIApplication sharedApplication] delegate]);
    UIWindow *window = delegate.window;
    if ([window.rootViewController isKindOfClass:[LMTabBarViewController class]]) {
        tabBarController = (LMTabBarViewController *)window.rootViewController;
    }
    return tabBarController;
}

@end
